package com.diudiutechnology.www.yadiudiu.yadiudiu.car.MyActivity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.view.ListViewForScrollView;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.BuyACarFragmentModel;
import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.util.thirdpay.AlipayClass;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.BannersAdapter;
import com.diudiutechnology.www.yadiudiu.yadiudiu.car.MyAdapter.ShopSelectServiceAdapter;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.TotalEvaluationAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/21.
 * 店铺详情
 */

public class ShopForDetailsActivity extends Activity implements View.OnClickListener {
    @Bind(R.id.retur)//返回
            LinearLayout retur;

    @Bind(R.id.select_service_listview)//精选服务
            ListViewForScrollView selectServiceListview;
    private List<BuyACarFragmentModel> selectServicelist;
    private ShopSelectServiceAdapter adapter;

    @Bind(R.id.contact)//支付
            TextView contact;

    @Bind(R.id.shop_evaluation_listview)//全部评价列表
            ListViewForScrollView shopEvaluationListview;
    List<BuyACarFragmentModel> shopEvaluationlist;
    TotalEvaluationAdapter evaluationAdapter;

    @Bind(R.id.all_evaluation)//查看全部评价
            TextView allEvaluation;

    @Bind(R.id.ad_pager)//轮播
            ViewPager adPager;
    @Bind(R.id.ad_layer)//圆圈
            LinearLayout adLayer;
    private final int HOME_AD_RESULT = 1;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                // 广告
                case HOME_AD_RESULT:
                    adPager.setCurrentItem(adPager.getCurrentItem() + 1,
                            true);
                    break;
            }
        }

        ;
    };
    Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_for_details);
        ButterKnife.bind(this);
        context = this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        contact.setOnClickListener(this);
        allEvaluation.setText(Html.fromHtml("查看全部评价<font color='#FF4537'>(157)</font>"));

        List<String> getinforresult = new ArrayList<>();
        getinforresult.add("1");
        getinforresult.add("1");
        getinforresult.add("1");
        getadvertising(getinforresult.size(), getinforresult);

        adapter = new ShopSelectServiceAdapter(this);
        selectServiceListview.setAdapter(adapter);
        evaluationAdapter = new TotalEvaluationAdapter(this);
        shopEvaluationListview.setAdapter(evaluationAdapter);
        initData();
    }

    private void initData() {
        selectServicelist = new ArrayList<>();
        BuyACarFragmentModel model = new BuyACarFragmentModel();
        model.setName("标准洗车");

        model.setLet("整车泡沫冲洗擦干，轮胎轮毂冲洗清洁，车内吸尘内饰脚垫等简单除尘");
        model.setMoney1("¥20.00");
        model.setMoney2("¥25.00");

        selectServicelist.add(model);
        model = new BuyACarFragmentModel();
        model.setName("全车精选");

        model.setLet("整车泡沫冲洗擦干，轮胎轮毂冲洗清洁，轮胎打蜡边逢清洗，发动机舱清洁，车内精致清洁");
        model.setMoney1("¥50.00");
        model.setMoney2("¥80.00");
        selectServicelist.add(model);

        model = new BuyACarFragmentModel();
        model.setName("全车打蜡");
        model.setLet("标准洗车、全车打蜡");
        model.setMoney1("¥70.00");
        model.setMoney2("¥100.00");
        selectServicelist.add(model);
        adapter.addItem(selectServicelist);


//        shopEvaluationlist = new ArrayList<>();
//        BuyACarFragmentModel models = new BuyACarFragmentModel();
//        models.setName("只是想改个名字");
//        models.setStar("2");
//        models.setDistance("标准洗车-五座轿车");
//        models.setDate("老板服务态度挺好的，洗车也洗得很彻底，里外都收 拾得挺干净的");
//        models.setPraise("2017.05.31");
//        shopEvaluationlist.add(models);
//        evaluationAdapter.addItem(shopEvaluationlist);
    }

    //轮播广告
    public void getadvertising(final int size, final List<String> getinforresult) {
        adPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                Sundry.refreshPoint(adLayer, position % size, 0);
                if (mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
                mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3 * 1000);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                if (ViewPager.SCROLL_STATE_DRAGGING == arg0
                        && mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
            }
        });
        BannersAdapter adapter = new BannersAdapter(context, getinforresult, 0);
        adPager.setAdapter(adapter);
        Sundry.initPointsLayout(adLayer, context, size, 0);
        adPager.setCurrentItem(size * 1000, false);
        // 自动轮播线程
        mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3 * 1000);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.contact:
                AlipayClass alipayClass = new AlipayClass(ShopForDetailsActivity.this, "测试", "测试1", getOutTradeNo(), 0.01, "");
                break;
        }
    }
        /**
         * 要求外部订单号必须唯一。
         * @return
         */
    private static String getOutTradeNo() {
        SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss", Locale.getDefault());
        Date date = new Date();
        String key = format.format(date);
        Random r = new Random();
        key = key + r.nextInt();
        key = key.substring(0, 15);
        return key;
    }
}
